package com.bootdo.contract.app;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.FileType;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.contract.domain.MissionDO;
import com.bootdo.contract.service.MissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bootdo.common.utils.MD5Utils;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.springframework.web.multipart.MultipartFile;

/*
 * app 公共访问方式
 */
@RestController
@RequestMapping("/app/open")
public class OpenController {
	private Logger logger = LoggerFactory.getLogger(OpenController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private BootdoConfig bootdoConfig;
	@Autowired
	private FileService sysFileService;

	ExecutorService executorService = Executors.newFixedThreadPool(20);


	@ResponseBody
	@PostMapping("/login")
	public R login(@RequestBody String json){
		logger.info("登录请求参数：{}",json);
		try {
			Map map = JSONObject.parseObject(json,Map.class);
			String userName = (String) map.get("username");
			String passWord = (String) map.get("password");
				UserDO user = new UserDO();
				user.setPassword(passWord);
				user.setUsername(userName);
				UserDO userdo = userService.getUserByUserInfo(user);
				// 账号不存在
				if (userdo == null) {
					return R.error(2,"账号或密码不正确");
				}
				// 密码错误
				if (!MD5Utils.encrypt(userName, passWord).equals(userdo.getPassword())) {
					return R.error(3, "账号或密码不正确");
				}
				// 账号锁定
				if (userdo.getStatus() == 0) {
					return R.error(4,"账号已被锁定,请联系管理员");
				}
				userdo.setToken(UUID.randomUUID().toString().replaceAll("-", ""));
				userService.update(userdo);
				logger.info("智慧外勤：{}",userdo.toString());
				return R.ok().put("user", userdo);
		} catch (Exception ex){
			logger.info("错误信息：{}",ex);
			return R.error(0001,"服务器异常");
		}
	}
	@ResponseBody
	@PostMapping("/uploadImg")
	public R login(@RequestParam("files") MultipartFile[] files){
		List<Long> urlList = new ArrayList<>();
		try {
			if(files != null){
				for (int i = 0; i < files.length; i++) {
					String fileName = files[i].getOriginalFilename();
					fileName = FileUtil.renameToUUID(fileName);
					FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
					FileUtil.uploadFile(files[i].getBytes(), bootdoConfig.getUploadPath(), fileName);
					sysFileService.save(sysFile);
					urlList.add(sysFile.getId());
				}
			}

		} catch (Exception ex){
			logger.info("错误信息：{}",ex);
			return R.error(0001,"服务器异常");
		}
		return R.ok().put("fileUrl",urlList);
	}


	@PostMapping("/testT")
	public void testT(){
		for (int i = 0; i < 50; i++) {
			executorService.submit(createThread());
		}
	}


	@SuppressWarnings("unchecked")
	private Thread createThread() {
		Thread thread = new Thread(new Runnable() {
			public void run() {
				System.out.println(System.currentTimeMillis());
			}
		});
		return thread;
	}
}
